<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="android mio4kon mio">
    <meta name="keyword"  content="">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
          Appium源码解析之一——建立连接 - Mio4kon的博客 | Mio4kon&#39;s Blog
        
    </title>

    <link rel="canonical" href="http://mio4kon.com/2017/06/12/Appium源码解析——建立连接/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/hux-blog.min.css">

    <!-- Pygments Highlight CSS -->
    <link rel="stylesheet" href="/css/highlight.css">

    <!-- Custom Fonts -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> -->
    <!-- Hux change font-awesome CDN to qiniu -->
    <link href="https://cdn.staticfile.org/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">


    <!-- Hux Delete, sad but pending in China
    <link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/
    css'>
    -->


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- ga & ba script hoook -->
    <script></script>
</head>


<!-- hack iOS CSS :active style -->
<body ontouchstart="">

    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">Mio4kon</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">Home</a>
                    </li>

                    

                        
                    

                        
                        <li>
                            <a href="/about/">About</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/archives/">Archives</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/tags/">Tags</a>
                        </li>
                        
                    
                    
                </ul>
            </div>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>


    <!-- Main Content -->
    
<!-- Image to hack wechat -->
<!-- <img src="http://mio4kon.com/img/icon_wechat.png" width="0" height="0"> -->
<!-- <img src="{{ site.baseurl }}/{% if page.header-img %}{{ page.header-img }}{% else %}{{ site.header-img }}{% endif %}" width="0" height="0"> -->

<!-- Post Header -->
<style type="text/css">
    header.intro-header{
        background-image: url('/img/home-bg.jpg')
    }
</style>
<header class="intro-header" >
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <div class="tags">
                        
                          <a class="tag" href="/tags/#移动自动化测试" title="移动自动化测试">移动自动化测试</a>
                        
                    </div>
                    <h1>Appium源码解析之一——建立连接</h1>
                    <h2 class="subheading">源码解析</h2>
                    <span class="meta">
                        Posted by Mio4kon on
                        2017-06-12
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

    <!-- Post Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

                <h1 id="本地运行Appium源码"><a href="#本地运行Appium源码" class="headerlink" title="本地运行Appium源码"></a>本地运行Appium源码</h1><ul>
<li>首先先下载 <code>Appium</code> 源码: <a href="https://github.com/appium/appium" target="_blank" rel="external">https://github.com/appium/appium</a></li>
<li>进入工程目录</li>
<li>执行 <code>npm install</code></li>
<li>执行 <code>node build/lib/main.js</code> </li>
</ul>
<h1 id="建立Appium连接"><a href="#建立Appium连接" class="headerlink" title="建立Appium连接"></a>建立Appium连接</h1><p>根据启动命令可以知道程序入口点在<code>lib/main.js</code>中.</p>
<p><strong>[appium:lib/main.js]</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">async function main (args = null) &#123;</div><div class="line">  ...</div><div class="line">  let router = getAppiumRouter(args);</div><div class="line">  let server = await baseServer(router, args.port, args.address);</div><div class="line">  ...</div><div class="line">  return server;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这里其实主要做了两件事.获取<code>router</code>和开启<code>server</code>.</p>
<p>让我们看<code>getAppiumRouter</code>到底做了什么?</p>
<p><strong>[appium:lib/appium.js]</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">function getAppiumRouter (args) &#123;</div><div class="line">  let appium = new AppiumDriver(args);</div><div class="line">  return routeConfiguringFunction(appium);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这里会生成<code>AppiumDriver</code>,然后将<code>driver</code>配置给<code>route</code>,那么<code>driver</code>具体是做什么的呢?</p>
<p>打开源码你会发现提供了各种方法,由于代码太多了.我们直接来看有哪些方法:</p>
<p><img src="http://mio4kon.qiniudn.com/14975180262189.jpg" alt=""></p>
<p>可以看出里面的方法都挺重要的.很多都是之后我们需要分析到的.现在我们先记住这个<code>AppiumDriver</code>可以给我提供类似<code>createSession</code>,<code>executeCommand</code>这些方法.</p>
<p>生成了<code>AppiumDriver</code>会把它传给<code>routeConfiguringFunction</code>.<code>routeConfiguringFunction</code>是<code>mjsonwp.js</code>的一个方法.我们看一下它的具体实现:</p>
<p><em>Tips: <code>routeConfiguringFunction</code>指向的是 <code>node_modules/appium-base-driver/build/lib/mjsonwp/mjsonwp.js</code>,但是<code>build</code>目录下都是最终生成的代码,会很多乱七八糟的东西会扰乱我们阅读源码.所以建议直接到<code>node_modules/appium-base-driver/lib/mjsonwp/mjsonwp.js</code>中看源码,后面源码的都是这种情况</em></p>
<p><strong>[appium-base-driver:lib/mjsonwp/mjsonwp.js]</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">function routeConfiguringFunction (driver) &#123;</div><div class="line">  ...</div><div class="line">  // return a function which will add all the routes to the driver</div><div class="line">  return function (app) &#123;</div><div class="line">    for (let [path, methods] of _.toPairs(METHOD_MAP)) &#123;</div><div class="line">      for (let [method, spec] of _.toPairs(methods)) &#123;</div><div class="line">        // set up the express route handler</div><div class="line">        buildHandler(app, method, path, spec, driver, isSessionCommand(spec.command));</div><div class="line">      &#125;</div><div class="line">    &#125;</div><div class="line">  &#125;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>主要逻辑都是在<code>buildHandler</code>中:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">function buildHandler (app, method, path, spec, driver, isSessCmd) &#123;</div><div class="line">  let asyncHandler = async (req, res) =&gt; &#123;</div><div class="line">    let jsonObj = req.body;</div><div class="line">    let httpResBody = &#123;&#125;;</div><div class="line">    let httpStatus = 200;</div><div class="line">    let newSessionId;</div><div class="line">    try &#123;</div><div class="line">      ...</div><div class="line">      if (driver.executeCommand) &#123;</div><div class="line">      //[1]</div><div class="line">        driverRes = await driver.executeCommand(spec.command, ...args);</div><div class="line">      &#125; else &#123;</div><div class="line">        driverRes = await driver.execute(spec.command, ...args);</div><div class="line">      &#125;</div><div class="line">      if (spec.command === &apos;createSession&apos;) &#123;</div><div class="line">        newSessionId = driverRes[0];</div><div class="line">        driverRes = driverRes[1];</div><div class="line">      &#125;</div><div class="line">  // add the method to the app</div><div class="line">  app[method.toLowerCase()](path, (req, res) =&gt; &#123;</div><div class="line">    B.resolve(asyncHandler(req, res)).done();</div><div class="line">  &#125;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这里面实际上就是服务器响应的处理逻辑,而逻辑是通过<code>[1]driver.executeCommand</code>来执行的.<br><code>executeCommand</code>方法实际上就是之前<code>AppiumDriver</code>中提供的方法.这个之后会具体分析.</p>
<p>那么<code>command</code>具体是什么? 发现它是取自<code>spec</code>中的.回到之前的<code>routeConfiguringFunction</code>方法.<br>可以看到<code>spec</code>其实都是来自于<code>METHOD_MAP</code>.</p>
<p><strong>[appium-base-driver:/lib/mjsonwp/routes.js]</strong></p>
<p><img src="http://mio4kon.qiniudn.com/14975204135809.jpg" alt=""></p>
<p>这个就不用太多解释了.根据客户端的不同请求来执行不同的<code>command</code>.<code>appium</code>正是这种<code>C/S架构</code>.</p>
<p>OK,到此为止我们已经知道了入口文件<code>main.js</code>中<code>getAppiumRouter(args)</code>方法具体的内容了.</p>
<p>现在我们来分析<code>await baseServer(router, args.port, args.address)</code>到底做了什么.其实猜都能猜到它开启了appium服务.</p>
<p><strong>[appium-base-driver:lib/express/server.js]</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">async function server (configureRoutes, port, hostname = null) &#123;</div><div class="line">  // create the actual http server</div><div class="line">  let app = express();</div><div class="line">  //[1]</div><div class="line">  let httpServer = http.createServer(app);</div><div class="line">  let close = httpServer.close.bind(httpServer);</div><div class="line">  httpServer.close = async () =&gt; &#123;</div><div class="line">    return await new Promise((resolve, reject) =&gt; &#123;</div><div class="line">      httpServer.on(&apos;close&apos;, resolve);</div><div class="line">      close((err) =&gt; &#123;</div><div class="line">        if (err) reject(err);</div><div class="line">      &#125;);</div><div class="line">    &#125;);</div><div class="line">  &#125;;</div><div class="line"></div><div class="line">  return await new Promise((resolve, reject) =&gt; &#123;</div><div class="line">    httpServer.on(&apos;error&apos;, (err) =&gt; &#123;</div><div class="line">    ...</div><div class="line">    //[2]</div><div class="line">    configureServer(app, configureRoutes);</div><div class="line">    let serverArgs = [port];</div><div class="line">    if (hostname) &#123;</div><div class="line">      // If the hostname is omitted, the server will accept</div><div class="line">      // connections on any IP address</div><div class="line">      serverArgs.push(hostname);</div><div class="line">    &#125;</div><div class="line">    httpServer.listen(...serverArgs, (err) =&gt; &#123;</div><div class="line">      if (err) &#123;</div><div class="line">        reject(err);</div><div class="line">      &#125;</div><div class="line">      resolve(httpServer);</div><div class="line">    &#125;);</div><div class="line">  &#125;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><code>[1]http.createServer(app)</code>,的确印证了我们的猜测–<strong>开启了一个服务</strong>.其中用了node的<a href="http://expressjs.com/zh-cn/" target="_blank" rel="external">express框架</a>,有兴趣的可以了解下.</p>
<p>再来看看<code>[2]configureServer(app, configureRoutes)</code>方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">function configureServer (app, configureRoutes) &#123;</div><div class="line"> </div><div class="line"></div><div class="line">  // set up static assets</div><div class="line">  app.use(favicon(path.resolve(STATIC_DIR, &apos;favicon.ico&apos;)));</div><div class="line">  app.use(express.static(STATIC_DIR));</div><div class="line"></div><div class="line">  // crash routes, for testing</div><div class="line">  app.use(&apos;/wd/hub/produce_error&apos;, produceError);</div><div class="line">  app.use(&apos;/wd/hub/crash&apos;, produceCrash);</div><div class="line">	...</div><div class="line">	//[1]</div><div class="line">  configureRoutes(app);</div><div class="line">	...</div><div class="line">  &#125;</div></pre></td></tr></table></figure>
<p>里面处理了各种异常,很重要一点绑定了之前创建的路由<code>[1]configureRoutes(app)</code>,<code>configureRoutes</code>就是之前我们获得<code>router</code>.</p>
<p><code>appium</code>服务器的创建以及响应的处理我们都已经在代码中一一找到了.下一篇将具体针对创建<code>session</code>这个请求来看一看整个过程是如何处理的.</p>
<p>下一篇地址: <a href="http://mio4kon.com/2017/06/23/Appium%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90%E4%B9%8B%E4%BA%8C%E2%80%94%E2%80%94%E7%AC%AC%E4%B8%80%E4%B8%AA%E8%AF%B7%E6%B1%82/">Appium源码解析之二——第一个请求</a></p>


                <hr>

                

                <ul class="pager">
                    
                        <li class="previous">
                            <a href="/2017/06/12/Appium踩坑之旅——小米手机/" data-toggle="tooltip" data-placement="top" title="Appium踩坑之旅——小米手机">&larr; Previous Post</a>
                        </li>
                    
                    
                        <li class="next">
                            <a href="/2017/04/13/移动端自动化测试系列之五——AppiumLich框架使用/" data-toggle="tooltip" data-placement="top" title="移动端自动化测试系列之五——AppiumLich框架使用">Next Post &rarr;</a>
                        </li>
                    
                </ul>

                

                
                <!-- disqus 评论框 start -->
                <div class="comment">
                    <div id="disqus_thread" class="disqus-thread"></div>
                </div>
                <!-- disqus 评论框 end -->
                

            </div>
    <!-- Side Catalog Container -->
        
            <div class="
                col-lg-2 col-lg-offset-0
                visible-lg-block
                sidebar-container
                catalog-container">
                <div class="side-catalog">
                    <hr class="hidden-sm hidden-xs">
                    <h5>
                        <a class="catalog-toggle" href="#">CATALOG</a>
                    </h5>
                    <ul class="catalog-body"></ul>
                </div>
            </div>
        

    <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                
                <section>
                    <!-- no hr -->
                    <h5><a href="/tags/">FEATURED TAGS</a></h5>
                    <div class="tags">
                       
                          <a class="tag" href="/tags/#移动自动化测试" title="移动自动化测试">移动自动化测试</a>
                        
                    </div>
                </section>
                

                <!-- Friends Blog -->
                
                <hr>
                <h5>FRIENDS</h5>
                <ul class="list-inline">

                    
                        <li><a href="http://lrd.ele.me/" target="_blank">lrd ele&#39;s Blog</a></li>
                    
                </ul>
                
            </div>

        </div>
    </div>
</article>




<!-- disqus 公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES * * */
    var disqus_shortname = "hexo-theme-huxblog";
    var disqus_identifier = "http://mio4kon.com/2017/06/12/Appium源码解析——建立连接/";
    var disqus_url = "http://mio4kon.com/2017/06/12/Appium源码解析——建立连接/";

    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<!-- disqus 公共JS代码 end -->




<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>
<!-- anchor-js, Doc:http://bryanbraun.github.io/anchorjs/ -->
<script>
    async("https://cdn.bootcss.com/anchor-js/1.1.1/anchor.min.js",function(){
        anchors.options = {
          visible: 'always',
          placement: 'right',
          icon: '#'
        };
        anchors.add().remove('.intro-header h1').remove('.subheading').remove('.sidebar-container h5');
    })
</script>
<style>
    /* place left on bigger screen */
    @media all and (min-width: 800px) {
        .anchorjs-link{
            position: absolute;
            left: -0.75em;
            font-size: 1.1em;
            margin-top : -0.1em;
        }
    }
</style>



    <!-- Footer -->
    <!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                
                
                

                
                    <li>
                        <a target="_blank" href="http://weibo.com/u/3198010790">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-weibo fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                
                    <li>
                        <a target="_blank"  href="https://github.com/Mio4kon">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; Mio4kon 2017 
                    <br>
                    Theme by <a href="http://huangxuan.me">Hux</a> 
                    <span style="display: inline-block; margin: 0 5px;">
                        <i class="fa fa-heart"></i>
                    </span> 
                    Ported by <a href="http://blog.kaijun.rocks">Kaijun</a> | 
                    <iframe
                        style="margin-left: 2px; margin-bottom:-5px;"
                        frameborder="0" scrolling="0" width="91px" height="20px"
                        src="https://ghbtns.com/github-btn.html?user=kaijun&repo=hexo-theme-huxblog&type=star&count=true" >
                    </iframe>
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/hux-blog.min.js"></script>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- 
     Because of the native support for backtick-style fenced code blocks 
     right within the Markdown is landed in Github Pages, 
     From V1.6, There is no need for Highlight.js, 
     so Huxblog drops it officially.

     - https://github.com/blog/2100-github-pages-now-faster-and-simpler-with-jekyll-3-0  
     - https://help.github.com/articles/creating-and-highlighting-code-blocks/    
-->
<!--
    <script>
        async("http://cdn.bootcss.com/highlight.js/8.6/highlight.min.js", function(){
            hljs.initHighlightingOnLoad();
        })
    </script>
    <link href="http://cdn.bootcss.com/highlight.js/8.6/styles/github.min.css" rel="stylesheet">
-->


<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("http://mio4kon.com/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>


<!-- Google Analytics -->




<!-- Baidu Tongji -->


<!-- Side Catalog -->

<script type="text/javascript">
    function generateCatalog (selector) {
        var P = $('div.post-container'),a,n,t,l,i,c;
        a = P.find('h1,h2,h3,h4,h5,h6');
        a.each(function () {
            n = $(this).prop('tagName').toLowerCase();
            i = "#"+$(this).prop('id');
            t = $(this).text();
            c = $('<a href="'+i+'" rel="nofollow">'+t+'</a>');
            l = $('<li class="'+n+'_nav"></li>').append(c);
            $(selector).append(l);
        });
        return true;    
    }

    generateCatalog(".catalog-body");

    // toggle side catalog
    $(".catalog-toggle").click((function(e){
        e.preventDefault();
        $('.side-catalog').toggleClass("fold")
    }))

    /*
     * Doc: https://github.com/davist11/jQuery-One-Page-Nav
     * Fork by Hux to support padding
     */
    async("/js/jquery.nav.js", function () {
        $('.catalog-body').onePageNav({
            currentClass: "active",
            changeHash: !1,
            easing: "swing",
            filter: "",
            scrollSpeed: 700,
            scrollOffset: 0,
            scrollThreshold: .2,
            begin: null,
            end: null,
            scrollChange: null,
            padding: 80
        });
    });
</script>





<!-- Image to hack wechat -->
<img src="http://mio4kon.com/img/icon_wechat.png" width="0" height="0" />
<!-- Migrate from head to bottom, no longer block render and still work -->

</body>

</html>
